﻿/*
 *  Sqlite ORM - PUBLIC DOMAIN LICENSE
 *  Copyright (C)  2010-2012. Ian Quigley
 *  
 *  This source code is provided 'As is'. You bear the risk of using it.
 */

using System.Collections.Generic;
using System.Linq;
using SqliteORM.Tests.Model;
using SubSpec;
using Xunit;

namespace SqliteORM.Tests
{
	public class DeleteWhere
	{
		[Specification]
		public void SimpleDeleteWhere()
		{
			TableIdString testOne = new TableIdString() { Id = 1, Text = "Hello" };
			TableIdString testTwo = new TableIdString() { Id = 2, Text = "Test Two" };
			"Given an table with some known data"
				.Context( ( ) =>
				          	{
				          		Context.Initialise();
				          		DbConnection.Initialise( Context.ConnectionString );
                                Context.DropTables();

								using (TableAdapter<TableIdString> adapter = TableAdapter<TableIdString>.Open())
								{
									adapter.CreateUpdate( testOne );
									adapter.CreateUpdate( testTwo );
								}
				          	} );

			"when you delete where known Id".Do( 
                () => {
                        using (TableAdapter<TableIdString> adapter = TableAdapter<TableIdString>.Open())
			            {
			            	adapter.Delete( Where.Equal( "Id", 1 ) );
                        }
			           } );

            "expect that the class cannot be found".Observation(
                () =>
                {
                    using (TableAdapter<TableIdString> adapter = TableAdapter<TableIdString>.Open())
						Assert.Null(adapter.Read(testOne.Id));                    
                });

            "expect that known data still exists".Observation(
                () =>
                {
                    using (TableAdapter<TableIdString> adapter = TableAdapter<TableIdString>.Open())
						Assert.NotNull(adapter.Read(testTwo.Id));               
                });
        }

		[Specification]
		public void DeleteWhereAllCondition()
		{
			List<TableIdString> known = new List<TableIdString>()
			                            	{
			                            		new TableIdString() {Id = 1, Text = "A"},
			                            		new TableIdString() {Id = 2, Text = "B"},
			                            		new TableIdString() {Id = 3, Text = "B"},
			                            		new TableIdString() {Id = 4, Text = "A"},
			                            		new TableIdString() {Id = 5, Text = "B"}
			                            	};

			"Given an table with known data".Context(
				() =>
				{
					Context.Initialise();
					DbConnection.Initialise(Context.ConnectionString);
                    Context.DropTables();

					using (TableAdapter<TableIdString> adapter = TableAdapter<TableIdString>.Open())
					{
						foreach (var row in known)
							adapter.CreateUpdate(row);
					}
				});
			"delete where Id > 1 and Text = 'B' and ID != 5 ".Do(
				() =>
				{
					using (TableAdapter<TableIdString> adpater = TableAdapter<TableIdString>.Open())
						adpater.Delete(Where.And(Where.GreaterThan("Id", 1), Where.Equal("Text", "B"), Where.NotEqual("Id", 5)));
				});

			"expect only 1, 4 and 5 remain".Observation(
				() =>
					{
						using (TableAdapter<TableIdString> adpater = TableAdapter<TableIdString>.Open())
						{
							Assert.NotNull( adpater.Read( 1 ) );
							Assert.NotNull( adpater.Read( 4 ) );
							Assert.NotNull( adpater.Read( 5 ) );

							Assert.Equal(3,  adpater.Select().Count() );
						}
					});
		}
	}	


}
